from os import execl, name
from numpy import true_divide
from numpy.lib.function_base import append
import pandas as pd; 
from pyecharts.charts import Sunburst;
from pyecharts import options as opts;

data = pd.read_excel('/Users/mac/Documents/project/python/first- project/data/数据集.xlsx')
data_all = [] # 处理过的树形结构
data_echarts = [] # 最终使用数据结构

# 判断数组中是否含有该名称, 若存在返回下标，若不存在返回-1
def isContainName(name, rows):
    # print(name, rows)
    for index, row in enumerate(rows):
        if row['name'] == name:
            return index
    return -1


# 处理excel数据，转换为树型数据结构
def dealExcelData(data):
    # print(data)
    for row in data.iterrows():
        data_row  = {}
        reWorldIndex = isContainName(row[1][0], data_all)
        if reWorldIndex != -1:
            reContentIndex = isContainName(row[1][1], data_all[reWorldIndex]['children'])
            if reContentIndex != -1:
                reCountryIndex = isContainName(row[1][2], data_all[reWorldIndex]['children'][reContentIndex]['children'])
                if reCountryIndex != -1:
                    print(reWorldIndex, reContentIndex, reCountryIndex, row)
                else:
                    data_row = {
                        'name': row[1][2],
                        'value': row[1][3] + row[1][4] + row[1][5],
                        'children': [
                            { 'name': 'total1', 'value': row[1][3], 'mark': 'mark' },
                            { 'name': 'total2', 'value': row[1][4], 'mark': 'mark' },
                            { 'name': 'total3', 'value': row[1][5], 'mark': 'mark' },
                        ]
                    }
                    data_all[reWorldIndex]['children'][reContentIndex]['children'].append(data_row)
                    data_all[reWorldIndex]['children'][reContentIndex]['value'] += row[1][3] + row[1][4] + row[1][5]
                    data_all[reWorldIndex]['value'] += row[1][3] + row[1][4] + row[1][5]
            else:
                data_row['name'] = row[1][1]
                data_row['value'] = row[1][3] + row[1][4] + row[1][5]
                data_row['children'] = [{
                    'name': row[1][2],
                    'children': [
                        { 'name': 'total1', 'value': row[1][3], 'mark': 'mark' },
                        { 'name': 'total2', 'value': row[1][4], 'mark': 'mark' },
                        { 'name': 'total3', 'value': row[1][5], 'mark': 'mark' },
                    ],
                    'value': row[1][3] + row[1][4] + row[1][5]
                }]
                data_all[reWorldIndex]['children'].append(data_row)
                data_all[reWorldIndex]['value'] += row[1][3] + row[1][4] + row[1][5]
        else:
            data_row = {
                'name': row[1][0],
                'value': row[1][3] + row[1][4] + row[1][5],
                'children': [{
                    'name': row[1][1],
                    'value': row[1][3] + row[1][4] + row[1][5],
                    'children': [{
                        'name': row[1][2],
                        'value': row[1][3] + row[1][4] + row[1][5],
                        'children': [
                            { 'name': 'total1', 'value': row[1][3], 'mark': 'mark' },
                            { 'name': 'total2', 'value': row[1][4], 'mark': 'mark' },
                            { 'name': 'total3', 'value': row[1][5], 'mark': 'mark' }
                        ],
                    }],
                }]
            }
            data_all.append(data_row)

# 树形数据结构转换为echrats格式
def transformEchartsData(data):
    if ('mark' in data):
        return opts.SunburstItem(
            name=data['name'],
            value=data['value']
        )
    else:
        arr = []
        for item in data['children']:
            arr.append(transformEchartsData(item))
        return opts.SunburstItem(
            name=data['name'],
            children=arr,
            value=data['value']
        )

dealExcelData(data)
data_echarts = [transformEchartsData(data_all[0]), transformEchartsData(data_all[1]),
    transformEchartsData(data_all[2]), transformEchartsData(data_all[3])]
# print(data)
# print(data_all)
# print(data_echarts)


sunburst = (
    Sunburst(init_opts=opts.InitOpts(width="1500px", height="800px"))
    .add(series_name="", data_pair=data_echarts, radius=[0, "100%"])
    .set_global_opts(title_opts=opts.TitleOpts(title="Sunburst-宇宙"))
    .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}"))
    .render("hahahahha.html")
)